<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>测试前端修复</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
        }
        .test-section {
            margin: 20px 0;
            padding: 20px;
            border: 1px solid #ddd;
            border-radius: 8px;
        }
        button {
            padding: 10px 20px;
            margin: 5px;
            border: none;
            border-radius: 4px;
            background: #007bff;
            color: white;
            cursor: pointer;
        }
        button:hover {
            background: #0056b3;
        }
        .log {
            background: #f8f9fa;
            padding: 10px;
            border-radius: 4px;
            margin: 10px 0;
            font-family: monospace;
            white-space: pre-wrap;
        }
    </style>
</head>
<body>
    <h1>前端修复测试</h1>
    
    <div class="test-section">
        <h2>测试1: 直接调用sendMessage()</h2>
        <button onclick="testDirectCall()">测试直接调用</button>
        <div id="log1" class="log"></div>
    </div>
    
    <div class="test-section">
        <h2>测试2: 模拟事件对象传递</h2>
        <button onclick="testEventObject()">测试事件对象</button>
        <div id="log2" class="log"></div>
    </div>
    
    <div class="test-section">
        <h2>测试3: 正常文本传递</h2>
        <button onclick="testNormalText()">测试正常文本</button>
        <div id="log3" class="log"></div>
    </div>

    <script>
        // 模拟sendMessage函数
        function sendMessage(text = null) {
            const log = document.getElementById('log1');
            
            // 调试：记录所有传入的参数
            console.log('sendMessage 被调用:', {
                text: text,
                textType: typeof text,
                textIsEvent: text && typeof text === 'object' && text.isTrusted,
                inputValue: 'test input'
            });
            
            // 如果传入的是事件对象，直接忽略
            if (text && typeof text === 'object' && text.isTrusted) {
                console.error('sendMessage: 检测到事件对象，直接忽略:', text);
                log.textContent += '❌ 检测到事件对象，已忽略\n';
                return;
            }
            
            // 确保message是字符串类型
            let message = text || 'test input'.trim();
            
            // 强化的事件对象检测
            if (typeof message === 'object' && message !== null) {
                // 检查是否是事件对象
                if (message.isTrusted || message.type || message.target || message.preventDefault) {
                    console.error('检测到事件对象，忽略:', message);
                    log.textContent += '❌ 检测到事件对象，已忽略\n';
                    return;
                }
                
                // 尝试提取text属性
                if (message.text) {
                    message = message.text;
                } else if (message.target && message.target.value) {
                    message = message.target.value;
                } else if (message.content) {
                    message = message.content;
                } else {
                    console.warn('无法从对象中提取消息内容:', message);
                    log.textContent += '❌ 无法提取消息内容\n';
                    return;
                }
            }
            
            // 确保最终是字符串并去除空白
            message = String(message || '').trim();
            
            // 调试信息
            console.log('发送消息:', {
                originalText: text,
                inputValue: 'test input',
                finalMessage: message,
                messageLength: message.length
            });
            
            if (!message) {
                console.warn('消息为空，取消发送');
                log.textContent += '❌ 消息为空\n';
                return;
            }
            
            log.textContent += `✅ 成功发送消息: "${message}"\n`;
        }
        
        // 测试函数
        function testDirectCall() {
            const log = document.getElementById('log1');
            log.textContent = '测试直接调用 sendMessage():\n';
            sendMessage();
        }
        
        function testEventObject() {
            const log = document.getElementById('log2');
            log.textContent = '测试事件对象传递:\n';
            
            // 模拟PointerEvent对象
            const mockEvent = {
                isTrusted: true,
                type: 'click',
                target: document.createElement('button'),
                preventDefault: function() {}
            };
            
            log.textContent += `传入模拟事件对象: ${JSON.stringify({
                isTrusted: mockEvent.isTrusted,
                type: mockEvent.type
            })}\n`;
            
            sendMessage(mockEvent);
        }
        
        function testNormalText() {
            const log = document.getElementById('log3');
            log.textContent = '测试正常文本传递:\n';
            sendMessage('Hello World');
        }
    </script>
</body>
</html>
